﻿using System;
using System.Collections.Generic;
using System.Text;
using com.dz.ct.common;
using System.Text.RegularExpressions;

namespace com.dz.ct.components.BigSite.www.qdmm.com
{
    public class BigSiteObject:IBigSiteObject
    {
        /// <summary>
        /// 获取首页所有的书籍
        /// </summary>
        /// <param name="i_book_default_url"></param>
        /// <returns></returns>
        public Dictionary<string, string> CollectDefaultBookList(string i_book_default_url)
        {
            return null;
        }
        ///http://all.qdmm.com/MMWeb/BookStore.aspx?ChannelId=41&SubCategoryId=-1&Tag=all&Size=-1&Action=-1&OrderId=6&P=all&PageIndex={0}&update=-1&Vip=-1
        /// <summary>
        /// 收集某个地址的所有小说详情地址
        /// </summary>
        /// <param name="i_book_list_url"></param>
        /// <returns>小说地址及小说名称</returns>
        public List<BookInfo> CollectBookList(string i_book_list_url)
        {
            string t_reg = "<span[^<>]*?class=[\'\"]?swbt[\'\"]?>\\s*?<a[^<>]*?hrefs*=s*[\'\"s]?([^\\d]*\\d{1,}[^\"]*)[\'\"]?[^<>]*?>(.*?)</a>.*? </span>.*?<div[^<>]*?class=[\'\"]?swe[\'\"]?>([^<>]*?)</div>";

            List<BookInfo> t_list = null;
            BookInfo t_book = null;
            DateTime t_update_time = DateTime.Now;
            try
            {
                string t_html = NetSiteCatchManager.ReadUrl(i_book_list_url, Encoding.GetEncoding("gb2312"));
                string t_url = string.Empty;
                if (!string.IsNullOrEmpty(t_html))
                {
                    MatchCollection t_ma = Regex.Matches(t_html, t_reg, RegexOptions.IgnoreCase | RegexOptions.Compiled);
                    if (t_ma != null && t_ma.Count > 0)
                    {
                        t_list = new List<BookInfo>();

                        foreach (Match t_mc in t_ma)
                        {
                            if (t_mc.Groups.Count >= 3)
                            {
                                t_book = new BookInfo();

                                t_book.BookName = t_mc.Groups[2].Value.ToString().Trim();
                                t_book.BookUrl = t_mc.Groups[1].Value.ToString().Trim();
                                if (!DateTime.TryParse(t_mc.Groups[3].Value.ToString().Trim(), out t_update_time))
                                    t_update_time = DateTime.Now;
                                t_book.LastUpdateTime = t_update_time;
                                t_list.Add(t_book);
                            }
                        }
                    }
                }

            }
            catch (Exception ex)
            {
                LogHelper.Error("收集小说列表地址失败" + ex.ToString());
            }

            return t_list;
        }

        /// <summary>
        /// 收集某本小说详细信息
        /// </summary>
        /// <param name="i_book_url"></param>
        /// <returns></returns>
        public BookInfo CollectBookItem(string i_book_url)
        {
            BookInfo t_book_info = new BookInfo();
            t_book_info.BookUrl = i_book_url;
            long t_site_book_id = 0;
            Match t_mc_site_book_id = Regex.Match(i_book_url, "(\\d+)\\.", RegexOptions.IgnoreCase | RegexOptions.Compiled);
            if (t_mc_site_book_id != null)
            {
                if (!long.TryParse(t_mc_site_book_id.Groups[1].Value.ToString(), out t_site_book_id))
                    t_site_book_id = 0;
            }
            t_book_info.BookChapterListUrl = string.Format("http://www.qdmm.com/bookReader/{0}.aspx", t_site_book_id);
            t_book_info.BookCommentUrl = string.Format("http://c.pingba.qdmm.com/BookCommentMM.aspx?BookId={0}", t_site_book_id);
            try
            {
                string t_html = NetSiteCatchManager.ReadUrl(i_book_url, Encoding.GetEncoding("gb2312"));
                if (!string.IsNullOrEmpty(t_html))
                {
                    Match t_mc = null;
             
                     t_mc = Regex.Match(t_html, "<a.*?>.*?起点女生网首页.*?</a>.*?<a.*?>(.*?)小说</a>().*?<a.*?>(.*?)</a>.*?<a.*?>(.*?)</a>", RegexOptions.Compiled | RegexOptions.IgnoreCase);
                  
                    if (t_mc != null && t_mc.Groups.Count > 2)
                    {
                        t_book_info.BookCategoryName = "女生小说";
                        t_book_info.BookSubCategroyName = t_mc.Groups[1].Value.ToString();
                        t_book_info.BookName = t_mc.Groups[3].Value.ToString();
                    }
                    t_mc = Regex.Match(t_html, "<div[^<>]*?classs*=s*[\'\"s]?pic_box[\'\"]?[^<>]*?>\\s*?<a.*?>\\s*?<img[^<>]*?srcs*=s*[\'\"s]?([^\"]*)[\'\"]?[^<>]*?>[^<>]*?</a>.*?小说作者.*?<a.*?>(.*?)</a>.*?<div[^<>]*?classs*=s*[\'\"s]?[data|tabs]*?[\'\"]?[^<>]*?>.*?<div[^<>]*?classs*=s*[\'\"s]?txt[\'\"]?[^<>]*?>(.*?)</div>", RegexOptions.IgnoreCase | RegexOptions.Compiled);
                    if (t_mc != null && t_mc.Groups.Count > 2)
                    {
                        t_book_info.BookImageUrl = t_mc.Groups[1].Value.ToString();
                        
                        t_book_info.BookAuthorName = t_mc.Groups[2].Value.ToString();
                        t_book_info.BookDescription = NetSiteCatchManager.ReplaceContent(t_mc.Groups[3].Value.ToString());
                   
                         t_mc = Regex.Match(t_html, "总点击[^<>]*?</b>(\\d{1,}).*?总推荐[^<>]*?</b>(\\d{1,}).*?().*?().*?写作进程.*?</b>(.*?)</td>", RegexOptions.Compiled | RegexOptions.IgnoreCase);
                       

                        if (t_mc != null && t_mc.Groups.Count > 4)
                        {
                            long t_click_count = 0;
                            if (!long.TryParse(t_mc.Groups[1].Value.ToString(), out t_click_count))
                                t_click_count = 0;
                            t_book_info.BookClick = t_click_count;
                            long t_collect = 0;
                            if (!long.TryParse(t_mc.Groups[2].Value.ToString(), out t_collect))
                                t_collect = 0;
                            t_book_info.BookCollect = t_collect;
                            if (!string.IsNullOrEmpty(t_mc.Groups[4].Value.ToString()) && t_mc.Groups[4].Value.ToString().ToLower().IndexOf("vip") > -1)
                            {
                                t_book_info.IsVip = true;
                            }
                           
                        }
                    }
                    t_book_info.SiteId = (int)Consts.Site.qdmm;

                }
            }
            catch (Exception ex)
            {
                LogHelper.Error("收集小说详情失败" + ex.ToString());
            }
           
        
            return t_book_info;
        }



        /// <summary>
        /// 收集某本书的所有章节
        /// </summary>
        /// <param name="i_chapter_list_url"></param>
        /// <returns></returns>
        public List<BookChapterInfo> CollectChapterList(string i_chapter_list_url)
        {
            List<BookChapterInfo> t_list_chapter = null;
            string t_volume_name = string.Empty;
            string t_sub_html = string.Empty;
            long t_site_book_id = 0;
            Match t_mc_book_id = Regex.Match(i_chapter_list_url, "(\\d+)\\.", RegexOptions.IgnoreCase | RegexOptions.Compiled);
            if (t_mc_book_id != null)
            {
                if (!long.TryParse(t_mc_book_id.Groups[1].Value.ToString(), out t_site_book_id))
                    t_site_book_id = 0;
            }
            if (t_site_book_id == 0)
                return null;
            bool t_is_vip = false;
            DateTime t_update_time = DateTime.Now;
            try
            {
                string t_html = NetSiteCatchManager.ReadUrl(i_chapter_list_url, Encoding.GetEncoding("gb2312"));
                if (!string.IsNullOrEmpty(t_html))
                {
                    t_list_chapter = new List<BookChapterInfo>();
                    string t_reg_chapter = "<li[^<>]*?styles*=s*[\'\"s]?[^\"]*[\'\"]?[^<>]*?><a[^<>]*?hrefs*=s*[\'\"s]?([^\"]*,(\\d{1,})\\.aspx).*?title=[\'\"]?[字]?[数]?[：]?([\\d+]*).*?更新时间：(\\d{4}-[\\d{2}|\\d{1}]+-[\\d{2}|\\d{1}]+\\s[\\d{2}|\\d{1}]+:[\\d{2}|\\d{1}]+:[\\d{2}|\\d{1}]+).*?[\'\"]*[\'\"]\\s*?>(.*?)</a>";
                    MatchCollection t_ma = Regex.Matches(t_html, t_reg_chapter, RegexOptions.IgnoreCase | RegexOptions.Compiled);

                    if (t_ma != null && t_ma.Count > 0)
                    {
                        foreach (Match t_mc in t_ma)
                        {

                            t_is_vip = false;
                            if (t_mc.Groups.Count >= 5)
                            {
                                BookChapterInfo t_book_chapter_info = new BookChapterInfo();

                                t_book_chapter_info.ChapterName = t_mc.Groups[5].Value.ToString();

                                if (!DateTime.TryParse(t_mc.Groups[4].Value.ToString(), out t_update_time))
                                    t_update_time = DateTime.Now;
                                t_book_chapter_info.UpdateTime = t_update_time;

                                if (t_mc.Groups[1].Value.ToString().ToLower().IndexOf("vip") > -1)
                                {
                                    t_is_vip = true;
                                }
                                t_book_chapter_info.IsVip = t_is_vip;

                                if (t_is_vip == false)
                                {
                                    t_book_chapter_info.ChapterUrl = string.Format("http://www.qdmm.com/BookReader/{0},{1}.aspx", t_site_book_id, t_mc.Groups[2].Value.ToString());
                                }
                                else
                                {
                                    t_book_chapter_info.ChapterUrl = string.Format("http://vipreader.qdmm.com/BookReader/vip,{0},{1}.aspx", t_site_book_id, t_mc.Groups[2].Value.ToString());
                                }

                                t_book_chapter_info.SiteId = (int)Consts.Site.qdmm;
                                t_list_chapter.Add(t_book_chapter_info);
                            }
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                LogHelper.Error(string.Format("收集小说:{0},章节失败:{1}", i_chapter_list_url, ex.ToString()));
            }
            //按跟新时间排序
            //if (t_list_chapter != null && t_list_chapter.Count > 0)
            //{
            //    t_list_chapter.Sort(delegate(BookChapterInfo t_a, BookChapterInfo t_b)
            //    {
            //        if (t_a.UpdateTime > t_b.UpdateTime)
            //            return 1;
            //        else if (t_a.UpdateTime == t_b.UpdateTime)
            //            return 0;
            //        return -1;
            //    });
            //}
            return t_list_chapter;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="i_volume_name"></param>
        /// <param name="i_text"></param>
        /// <param name="i_site_book_id"></param>
        /// <returns></returns>
        private List<BookChapterInfo> ParseChapter(string i_volume_name, string i_text, long i_site_book_id)
        {
            string t_reg_chapter = "<li[^<>]*?styles*=s*[\'\"s]?[^\"]*[\'\"]?[^<>]*?><a[^<>]*?hrefs*=s*[\'\"s]?([^\"]*,(\\d{1,})\\.aspx).*?title=[\'\"]?[字]?[数]?[：]?([\\d+]*).*?更新时间：(\\d{4}-[\\d{2}|\\d{1}]+-[\\d{2}|\\d{1}]+\\s[\\d{2}|\\d{1}]+:[\\d{2}|\\d{1}]+:[\\d{2}|\\d{1}]+).*?[\'\"]*[\'\"]\\s*?>(.*?)</a>";

            List<BookChapterInfo> t_chapter_list = new List<BookChapterInfo>();
            try
            {
                //匹配的集合
                MatchCollection t_ma = null;
          
                i_volume_name = i_volume_name.Replace("&nbsp;", "");
                i_volume_name = Regex.Replace(i_volume_name, "(\\[.*?\\])", "", RegexOptions.IgnoreCase | RegexOptions.Compiled);
                //更新时间
                DateTime t_update_time;

                bool t_is_vip = false;

                t_ma = Regex.Matches(i_text, t_reg_chapter, RegexOptions.IgnoreCase | RegexOptions.Compiled);

                if (t_ma != null && t_ma.Count > 0)
                {
                    foreach (Match t_mc in t_ma)
                    {

                        t_is_vip = false;
                        if (t_mc.Groups.Count >= 5)
                        {
                            BookChapterInfo t_book_chapter_info = new BookChapterInfo();
                      
                            t_book_chapter_info.ChapterName = t_mc.Groups[5].Value.ToString();

                            

                            if (!DateTime.TryParse(t_mc.Groups[4].Value.ToString(), out t_update_time))
                                t_update_time = DateTime.Now;
                            t_book_chapter_info.UpdateTime = t_update_time;

                            if (t_mc.Groups[1].Value.ToString().ToLower().IndexOf("vip") > -1)
                            {
                                t_is_vip = true;
                            }
                            t_book_chapter_info.IsVip = t_is_vip;

                            if (t_is_vip == false)
                            {
                                t_book_chapter_info.ChapterUrl = string.Format("http://www.qdmm.com/BookReader/{0},{1}.aspx", i_site_book_id, t_mc.Groups[2].Value.ToString());
                            }
                            else
                            {
                                t_book_chapter_info.ChapterUrl = string.Format("http://vipreader.qdmm.com/BookReader/vip,{0},{1}.aspx", i_site_book_id, t_mc.Groups[2].Value.ToString());
                            }

                            t_book_chapter_info.SiteId = (int)Consts.Site.qdmm;
                            t_chapter_list.Add(t_book_chapter_info);
                        }
                    }
                }

            }
            catch (Exception ex)
            {
                LogHelper.Error("收录章节失败" + ex.ToString());
            }
            return t_chapter_list;
        }

        /// <summary>
        /// 收集章节内容
        /// </summary>
        /// <param name="i_chapter_content_url"></param>
        /// <returns></returns>
        public string CollectChapterContent(string i_chapter_content_url)
        {
            if (string.IsNullOrEmpty(i_chapter_content_url))
                return string.Empty;

         
            string t_reg_content = "document.write\\('(.*?)\'\\)";
            string t_chapter_content = string.Empty;
            try
            {
                string t_html = NetSiteCatchManager.ReadUrl(i_chapter_content_url, Encoding.GetEncoding("gb2312"));

                if (!string.IsNullOrEmpty(t_html))
                {

                    Match t_mc_content = Regex.Match(t_html, t_reg_content, RegexOptions.IgnoreCase | RegexOptions.Compiled);
                    if (t_mc_content != null && t_mc_content.Groups.Count > 0)
                    {
                        t_chapter_content = t_mc_content.Groups[1].Value.ToString();
                        t_chapter_content = NetSiteCatchManager.ReplaceContent(t_chapter_content);
                        t_chapter_content = string.Format("document.write('{0}');", t_chapter_content);
                    }
                }

                return t_chapter_content;
            }
            catch (Exception ex)
            {
                LogHelper.Error("获取章节内容失败" + ex.ToString());
                return string.Empty;
            }
        }



        private string ParseContent(string i_conent)
        {
            if (string.IsNullOrEmpty(i_conent))
                return string.Empty;

            //替换掉所有的Html标记
            string t_conent = Regex.Replace(i_conent, "<[^\u4e00-\u9fa5]>", "", RegexOptions.IgnoreCase | RegexOptions.Compiled).Trim();

            if (t_conent.Length > 250)
                return i_conent;
            return string.Empty;
        }

        /// <summary>
        /// 收集小说评论
        /// </summary>
        /// <param name="i_comment_url"></param>
        /// <returns></returns>
        public List<string> CollectReview(string i_comment_url)
        {
            List<string> t_comment_list=new List<string>();
            try
            {
                string  t_html = NetSiteCatchManager.ReadUrl(i_comment_url, Encoding.GetEncoding("gb2312"));

                if (!string.IsNullOrEmpty(t_html))
                {
                    MatchCollection t_ma = Regex.Matches(t_html, "<div[^<>]*?class=[\'\"]title[\'\"]?>.*?</div>\\s*?<div[^<>]*?class=[\'\"]?cont[\'\"]?\\s*?>(.*?)</div>", RegexOptions.IgnoreCase | RegexOptions.Compiled);

                    if (t_ma != null && t_ma.Count > 0)
                    {
                      
                        foreach (Match t_mc_review in t_ma)
                        {
                            string t_content = t_mc_review.Groups[1].Value.ToString();
                            t_content = Regex.Replace(t_content, "<.*?>.*?<.*?>", "", RegexOptions.IgnoreCase | RegexOptions.Compiled);
                            t_content = Regex.Replace(t_content, "<.*?>", "", RegexOptions.IgnoreCase | RegexOptions.Compiled);
                            t_content = Regex.Replace(t_content, "\\d+起点币", "", RegexOptions.IgnoreCase | RegexOptions.Compiled);
                            t_content = Regex.Replace(t_content, "起点中文网", "", RegexOptions.IgnoreCase | RegexOptions.Compiled);
                            t_content = Regex.Replace(t_content, "[a-zA-Z0-9]", "", RegexOptions.IgnoreCase | RegexOptions.Compiled);
                            if (string.IsNullOrEmpty(t_content) || t_content.Length < 10)
                                continue;
                           t_comment_list.Add(t_content);
                        }
                    }
                }

                return t_comment_list;
               
            }
            catch (Exception ex)
            {
                LogHelper.Error("获取评论失败" + ex.ToString());
                return null;
            }
        }
    }
}
